<template>
  <div class="app-container">
    <el-form :inline="true" :model="queryParams" label-width="68px" ref="queryForm" size="small"
             style="text-align: left" v-show="showSearch">
      <el-form-item label="套餐名称" prop="taocanName">
        <el-input
            @keyup.enter.native="handleQuery"
            clearable
            placeholder="请输入套餐名称"
            style="width: 240px"
            v-model="queryParams.taocanName"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" icon="Search" size="mini" type="warning">搜索</el-button>
        <el-button @click="resetQuery" icon="Refresh" size="mini">重置</el-button>
      </el-form-item>
    </el-form>

    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>体检套餐</span>
          <el-button class="button" text>Physical Examination Appointment Records</el-button>
        </div>
      </template>
      <el-table :data="list" row-key="userId" v-loading="loading">
        <el-table-column align="center" type="index" width="50"/>
        <el-table-column align="center" label="图片" prop="cover" width="120">
          <template #default="scope">
            <el-image :src="scope.row.cover" style="width: 80px; height: 80px;border-radius: 10px;"></el-image>
          </template>
        </el-table-column>

        <el-table-column align="center" label="医院名称" prop="hospital"/>
        <el-table-column align="center" label="套餐名称" prop="taocanName"/>
        <el-table-column align="center" label="体检地点" prop="address"/>
        <el-table-column align="center" label="科室名称" prop="roomName"/>
        <el-table-column align="center" label="价格" prop="price"/>
        <el-table-column
            align="center"
            class-name="small-padding fixed-width"
            label="操作"
            width="160"
        >
          <template #default="scope">
            <el-button
                @click="keep(scope.row)"
                icon="Pointer"
                size="mini"
                style="color: #3cb71c"
                type="text"
            >详情
            </el-button>
            <el-button
                @click="addSubscribe(scope.row)"
                icon="Plus"
                size="mini"
                style="color: #FFB101"
                type="text"
            >预约
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-drawer
        ref="drawerRef"
        v-model="dialog"
        title="体检预约"
        direction="rtl"
        class="demo-drawer"
    >
      <div style="padding: 20px 20px">
        <el-card class="box-card" style="text-align: left;line-height: 35px;margin: 20px 0px">
          <h3>{{ detail.hospital }}</h3>
          <p><el-icon><Location /></el-icon>{{ detail.address }}</p>
          <p style="color: #42b983"><el-icon><CircleCheck /></el-icon>购买后立即生效，请按时到院进行体检</p>
        </el-card>

        <el-card class="box-card" style="text-align: left;line-height: 35px">
          <el-form :model="form" ref="ruleFormRef" :rules="rules">
            <el-form-item label="体检日期" prop="time">
              <el-date-picker
                  v-model="form.time"
                  type="date"
                  placeholder="请选择体检日期"
              />
            </el-form-item>
            <el-form-item label="就诊人" prop="userName">
              <el-input v-model="form.userName" autocomplete="off" />
            </el-form-item>
            <el-form-item label="身份证号" prop="idCard">
              <el-input v-model="form.idCard" autocomplete="off" />
            </el-form-item>
            <el-form-item label="联系电话" prop="userPhone">
              <el-input v-model="form.userPhone" autocomplete="off" />
            </el-form-item>
          </el-form>
          <div style="display: flex;justify-content: space-between;padding: 0px 30px">
<!--            <span style="color: red;font-size: 22px;font-weight: bold">￥248.00</span>-->
            <el-button type="success" @click="submitForm">立即预约</el-button>
          </div>
        </el-card>
      </div>
    </el-drawer>

    <el-drawer
        ref="drawerRef"
        v-model="dialog2"
        title="体检套餐详情"
        direction="rtl"
        class="demo-drawer"
    >
      <div style="padding: 20px 20px">
        <el-card class="box-card" style="text-align: left;line-height: 35px;margin: 20px 0px">
          <h3>{{ detail.hospital }}</h3>
          <p><el-icon><Location /></el-icon>{{detail.address}}</p>
<!--          <p style="color: #42b983" v-if="detail.status==='1'"><el-icon><CircleCheck /></el-icon>你已成功预约体检，请准备好相关证件材料准时参加体检！</p>-->
<!--          <p style="color: #c20f2a" v-if="detail.status==='2'"><el-icon><CircleCheck /></el-icon>您的体检预约申请失败，您可选择其他时间再次预约！</p>-->
        </el-card>

        <el-card class="box-card" style="text-align: left;line-height: 35px">
          <el-descriptions title="体检中心信息" column="1">
            <el-descriptions-item label="套餐名称">{{detail.taocanName}}</el-descriptions-item>
            <el-descriptions-item label="科室名称">{{detail.roomName}}</el-descriptions-item>
            <el-descriptions-item label="价格">{{detail.price}}</el-descriptions-item>
          </el-descriptions>
        </el-card>
      </div>
    </el-drawer>

  </div>
</template>

<script>

export default {
  name: "PhyTaocan",
  data() {
    return {
      detail:{},
      dialog2:false,
      dialog:false,
      dialogFormVisible:false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taocanName: '',
      },
      // 表单校验
      rules: {
        time: [
          {required: true, message: "请选择体检时间", trigger: "blur"},
        ],
        userName: [
          {required: true, message: "用户名称不能为空", trigger: "blur"},
          {min: 2, max: 12, message: '用户名称长度必须介于 2 和 12 之间', trigger: 'blur'}
        ],
        userPhone: [
          {required: true, message: "用户手机号不能为空", trigger: "blur"},
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        idCard: [
          {required: true, message: "用户手机号不能为空", trigger: "blur"},
          {
            pattern: /^[1-9]\d{5}(19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[Xx\d]$/,
            message: "请输入正确的身份证号",
            trigger: "blur"
          }
        ],
      }
    };
  },
  created() {
    this.getTaocanList();
  },
  methods: {

    keep(row){
      this.detail=row;
      this.dialog2=true;
    },

    addSubscribe(row){
      this.detail=row;
      this.reset();
      this.dialog=true;
    },

    async getTaocanList() {
      this.loading = true;
      const {data: res} = await this.$http.get('taocan/list', {params:this.queryParams})
      console.log(res)
      this.list = res.data
      // this.total = res.data.total
      this.loading = false
    },

    // 表单重置
    reset() {
      this.form = {
        id: 0,
        time: "",
        userName: "",
        userPhone: "",
        status: "0",
        idCard: "",
        taocanId: this.detail.id,
        taocanName: this.detail.taocanName,
        hospital: this.detail.hospital
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getTaocanList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // 查询参数
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        userPhone: "",
      },
          this.getTaocanList();
    },
    /** 修改按钮操作 */
    async handleUpdate(row) {
      console.log(row)
      this.form = JSON.parse(JSON.stringify(row));
      this.title = "更新用户信息";
      this.dialogFormVisible = true;
    },
    /** 提交按钮 */
    async submitForm() {
      debugger;
      await this.$refs["ruleFormRef"].validate(valid => {
        if (valid) {
          this.addFromFun();
        }
      });
      this.dialog = false;
    },
    /**
     * 添加
     * */
    async addFromFun() {
      const {data: res} = await this.$http.post('appointment/insertAppointment', this.form);
      console.log(res)
      if (res.code === 200) {
        this.$message.success('预约申请成功，将在三个工作日给出结果，注意查看！')
        this.getTaocanList();
      } else if (res.code === 50003) {
        this.$message.error('您有预约未完成，请勿多次预约！')
      } else {
        this.$message.error('服务器异常')
      }
    }
  }
};
</script>
